package me.xiaoyan.ehr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.xiaoyan.ehr.mapper.AccountMapper;
import me.xiaoyan.ehr.mapper.EmployeeMapper;
import me.xiaoyan.ehr.po.Employee;
import me.xiaoyan.ehr.service.EmployeeService;
import me.xiaoyan.ehr.vo.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.Map;

@Service
public class EmployeeServiceImpl extends BaseService<EmployeeMapper, Employee> implements EmployeeService {
    private AccountMapper accountMapper;

    @Autowired
    public void setAccountMapper(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    @Override
    public IPage<Employee> selectByParams(Map<String, String> params) {
        return selectByPage(params, null);
    }

    @Override
    public Employee update(Employee employee) {
        return null;
    }



    @Override
    public Employee create(Employee employee) {
        // 判断是否存在工号
        if (this.getByEmpId(employee.getEmpId()) != null) {
            throw new BizException("员工编号已经存在");
        }
        // 判断是否已经存在身份证
        if (this.getByIdCardNumber(employee.getIdCardNumber()) != null) {
            throw new BizException("身份证号码已经存在");
        }

        return this.save(employee) ? employee : null;
    }

    @Override
    public Employee getByEmpId(String empId) {
        QueryWrapper<Employee> query = new QueryWrapper();
        query.eq("emp_id",empId);
        return getOne(query);
    }

    @Override
    public Employee getByIdCardNumber(String idCardNumber) {
        QueryWrapper<Employee> query = new QueryWrapper();
        query.eq("id_card_number",idCardNumber);
        return getOne(query);
    }
}
